package cn.fuzongyao.learning.android;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Array;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class QuizActivity03 extends AppCompatActivity {
    private static final String TAG = "QuizActivity";
    private TextView questionTextView;
    private Button trueButton;
    private Button falseButton;
    private int currentIndex = 0;
    private static final String INDEX_KEY = "index";
    private Question[] questions = new Question[]{
            new Question(R.string.question_australia, true),
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, true),
            new Question(R.string.question_africa, true),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true)
    };
    private List<Boolean> answers;
    private int trueAnswers = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ----------------------");
        setContentView(R.layout.activity_quiz);
        if (savedInstanceState != null) {
            currentIndex = savedInstanceState.getInt(INDEX_KEY, 0);
        }

        questionTextView = findViewById(R.id.question_text_view);
        trueButton = findViewById(R.id.true_button);
        falseButton = findViewById(R.id.false_button);
        updateQuestion();

        questionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % questions.length;
                updateQuestion();
            }
        });

        trueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        falseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });

        View.OnClickListener nextClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % questions.length;
                updateQuestion();
            }
        };
        Button nextButton = findViewById(R.id.next_button);
        nextButton.setOnClickListener(nextClickListener);

        View.OnClickListener prevClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + questions.length - 1) % questions.length;
                updateQuestion();
            }
        };
        Button prevButton = findViewById(R.id.prev_button);
        prevButton.setOnClickListener(prevClickListener);
    }

    private void updateQuestion() {
        questionTextView.setText(questions[currentIndex].getTextResId());
        isAnswered();
    }

    private void isAnswered() {
        if (answers == null) {
            answers = new ArrayList<>(questions.length);
            for (int i = 0; i < questions.length; i++) {
                answers.add(null);
            }
        }
        if (answers.get(currentIndex) != null) {
            trueButton.setEnabled(false);
            falseButton.setEnabled(false);
        } else {
            trueButton.setEnabled(true);
            falseButton.setEnabled(true);
        }
    }

    private void checkAnswer(boolean userPressedTrue) {
        answers.set(currentIndex, userPressedTrue);
        isAnswered();
        boolean answerTrue = questions[currentIndex].isAnswerTrue();
        int messageResId = 0;
        if (answerTrue == userPressedTrue) {
            trueAnswers++;
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
        double score = trueAnswers * 10000 / questions.length / 100.0;
        String scoreToast = this.getString(R.string.score_toast) + score + "%";
        Log.d(TAG, scoreToast);

        if (answers.stream().allMatch(Objects::nonNull)) {
            Toast.makeText(this, scoreToast, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState: ----------------------");
        outState.putInt(INDEX_KEY, currentIndex);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ----------------------");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ----------------------");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ----------------------");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ----------------------");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ----------------------");
    }
}
